package com.roger.uia;

import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;

import com.roger.uia.helper.UiBase;
import com.roger.uia.helper.UiWatch;
import com.roger.uia.helper.util.FileUtils;
import com.roger.uia.helper.util.ZipUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

import static com.roger.uia.helper.UiBase.mDevice;
import static com.roger.uia.helper.UiBase.waitTime;

public class DealLogs {
    private final String TAG = DealLogs.class.getSimpleName();

    @Before
    public void setUp()  {
        FileUtils.getPermission();
        UiWatch.getWatcher().runErrorWatcher();
        UiWatch.getWatcher().runSpecialWatcher();
    }

    @After
    public void tearDown() {
        UiWatch.getWatcher().removeErrorWatcher();
        UiWatch.getWatcher().removeSpecialWatcher();
    }

    @Test
    public void init() {
        String FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DockState/log.txt";
        String MTK_PAC = "com.mediatek.mtklogger/com.mediatek.mtklogger.MainActivity";
        String START_STOP_BTN = "com.mediatek.mtklogger:id/startStopToggleButton";
        String OK_BTN = "android:id/button1";
        String logPath = UiBase.filePath +"mtklog";
        int i = 0 ;
        while(i < 1000){
            Log.d( TAG, String.valueOf(i));
            try {
                if(!UiBase.existObj(UiBase.objSelect[3], START_STOP_BTN)) UiBase.startApp(MTK_PAC);
                UiBase.waitObjCheck(UiBase.objSelect[3], START_STOP_BTN,10);
                if(UiBase.objChecked(UiBase.objSelect[3], START_STOP_BTN)) {
                    UiBase.clickObj(UiBase.objSelect[3], START_STOP_BTN);
                    UiBase.waitClickIfObjExist(UiBase.objSelect[3], OK_BTN,3);
                }
                UiBase.waitObj(UiBase.objSelect[3], START_STOP_BTN,30);

                if(FileUtils.existFile(logPath) && i > 0){
                    String logTime = UiBase.dateFormat.format(new Date(System.currentTimeMillis()));
                    ZipUtils.zip().zipFile(logPath,UiBase.filePath+"logs_"+ logTime + ".zip");
                }

//                if(FileUtils.existFile(FILE_PATH)){
//                    int m = readTxtC(FILE_PATH);
//                    if( m > i ){
//                        Log.d(TAG,"disconnected happened again,start zip logs");
//                        i = m ;
//                        String logTime = UiBase.dateFormat.format(new Date(System.currentTimeMillis()));
//                        ZipUtils.zip().zipFile(logPath,UiBase.filePath+"logs_"+ logTime + ".zip");
//                        Log.d(TAG,"zip logs end");
//                        SystemClock.sleep(waitTime);
//                    }else{
//                        Log.d(TAG,"not happen disconnected during this time");
//                    }
//                }
                mDevice.executeShellCommand("rm -rf "+ logPath);

                if(!UiBase.existObj(UiBase.objSelect[3], START_STOP_BTN)) UiBase.startApp(MTK_PAC);
                UiBase.waitObjCheck(UiBase.objSelect[3], START_STOP_BTN,10);
                if(!UiBase.objChecked(UiBase.objSelect[3], START_STOP_BTN)) {
                    UiBase.clickObj(UiBase.objSelect[3], START_STOP_BTN);
                    UiBase.waitClickIfObjExist(UiBase.objSelect[3], OK_BTN,3);
                }
                mDevice.pressHome();
                Log.d(TAG,"wait 2 hours");
                SystemClock.sleep(waitTime * 3600 * 2 );
                i++;
            } catch (Throwable e) {
                mDevice.pressHome();
                e.printStackTrace();
            }
        }
    }

    private int readTxtC(String path){
        int i=0;
        try {
            InputStream in = new FileInputStream(path);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String line ="";
            while((line=br.readLine())!=null){
                if(line.contains("disconnected")) i++;
            }
            br.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return i;
    }
}
